// 这里的文件引入是在uView里面的，官方文档：https://www.uviewui.com/
// 很多方法与uVie的使用一致，详见：https://www.uviewui.com/js/intro.html
import "./utils/index.js";
import { showModal ,showToast} from "./utils/util";
import { getStorageItem,setStorageItem } from "./utils/storage";
import { Token } from "./utils/constant";
// 引入全局路由
import routes, { noIntercept } from "./config/routes.config.js";
import apiConfig from "./config/api.config.js";
import httpApi from "./common/http.api";
wx.$rConfig = routes; //  全局路由
wx.$apiConfig = apiConfig; // 全局请求路径 api
wx.$api = httpApi; // 将各个定义的接口名称，统一放进对象挂载到vm.$api下
import Dialog from '@vant/weapp/dialog/dialog';
import Notify from '@vant/weapp/notify/notify';
// 路由拦截
wx.$u.routeIntercept = (params, resolve) => {
	if (
		noIntercept.some((v) => {
			return params.url.includes(v);
		})
	) {
		// 无需校验登陆状态
		resolve(true);
	} else {
		// 需要校验
		const token = getStorageItem('token') || "";
		if (token) {
			resolve(true);
		} else {
			Dialog.confirm({
				title: '您还未登录',
				message: '请先登录再进行操作',
			  }).then(() => {
				getApp().getUserLogin(params.url)
			  }).catch(() => {});
			// showModal({
			// 	content: "需要登陆才可以访问哟",
			// 	confirmText: "立即登录",
			// 	failText:'暂不登录',
			// }).then((res) => {
			// 	if (res) {
			// 		getApp().getUserLogin()
			// 		wx.$u.router(routes.systemMessage);
			// 	}
			// });
			resolve(false);
		}
	}
};

App({
	// 全局数据
	globalData: {
		userInfo: null,
		token: getStorageItem('token') || "",
	},
	onLaunch() {
		
	},
	getUserLogin(url){
		let that = this
		wx.login({
		  success(loginRes) {
			if (loginRes.code) {
			  // 登录接口
			  let obj = {
				code:loginRes.code
			  }
			  wx.$api.getToken(obj).then(res => {
				setStorageItem('token',res.token)
				setStorageItem('user_type',res.user_type)
				wx.$u.router(url);
			  })
			} else {
			  console.error('登录失败！' + loginRes.errMsg)
			}
		  }
		})
	}
});
